package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.*;
import com.example.springboot.entity.Grade;
import com.example.springboot.service.EnrollService;
import com.example.springboot.service.GradeService;
import com.example.springboot.service.NewsService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 功能：成绩相关
 * 作者：刘涵茹
 * 日期：2024/4/1 14:38
 */
@RestController
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    UserService userService;
    @Autowired
    EnrollService enrollService;
    @Autowired
    NewsService newsService;
    @Autowired
    GradeService gradeService;

    /**
     * 新增信息 post
     */
    @PostMapping("/add")
    @HoneyLogs(operation = "成绩", type = LogType.ADD)
    public Result add(@RequestBody Grade grade) {
        try { 
            grade.setCreatedtime(DateUtil.today());
            Enroll enroll = enrollService.getById(grade.getEnrollid());
            User user = userService.getByPartId(enroll.getPartid());
            grade.setTitle(enroll.getTitle());
            grade.setSign(enroll.getSign());
            grade.setTeacher(enroll.getTeacher());
            grade.setMajor(user.getMajor());
            grade.setPart(user.getUsername());
            gradeService.save(grade);
            enroll.setGrade("已评分");
            enrollService.updateById(enroll);
        }catch (Exception e) {
            return Result.error("系统错误");//其他报错
        }
        return Result.success();
    }

//    /**
//     * 修改信息 put
//     */
//    @PutMapping("/update")
//    @HoneyLogs(operation = "成绩", type = LogType.UPDATE)
//    public Result update(@RequestBody Grade grade) {
//        try {
//            gradeService.updateById(grade);//service中的函数
//        } catch (Exception e) {
//            return Result.error("系统错误");//其他报错
//        }
//        return Result.success();
//    }

//    /**
//     * 单个删除信息 delete
//     */
//    @HoneyLogs(operation = "报名", type = LogType.DELETE)
//    @DeleteMapping("/delete/{id}")//delete需要通过path相关注解使用
//    public Result delete(@PathVariable Integer id) {
//        gradeService.removeById(id);//service中的函数
//        return Result.success();
//    }

    /**
     * 查询全部的信息
     */
    @GetMapping("/selectAll")
    public Result selectAll() {//得到数组，并传到service中
        List<Grade> gradeList = gradeService.list(new QueryWrapper<Grade>().orderByDesc("id"));
        //service中的函数
        //select * from enroll order by id desc
        return Result.success(gradeList);
    }

    /**
     * 单个条件查询
     * 根据id查询单个的信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {//得到数组，并传到service中
        Grade grade = gradeService.getById(id);//service中的函数
        return Result.success(grade);
    }


    /**
     * 分页多个条件模糊查询
     * 查询所有角色的信息
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam Integer pageNum,//页数或者页码
            @RequestParam Integer pageSize, //当前页总共有多少条数据 这两个都是前端传参过来的
            @RequestParam String title,
            @RequestParam String sign,
            @RequestParam String teacher,
            @RequestParam String part,
            @RequestParam String major
    ) {//得到数组，并传到service中
        QueryWrapper<Grade>queryWrapper = new QueryWrapper<Grade>().orderByDesc("id");//默认倒序,让最新的数据在最上面
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.like(StrUtil.isNotBlank(sign), "sign", sign);
        queryWrapper.like(StrUtil.isNotBlank(teacher), "teacher", teacher);
        queryWrapper.like(StrUtil.isNotBlank(part), "part", part);
        queryWrapper.like(StrUtil.isNotBlank(major), "major", major);
        Page<Grade> page = gradeService.page(new Page<>(pageNum, pageSize), queryWrapper);//service中的函数
        List<Grade> records = page.getRecords();
        for (Grade recode : records){
            Integer enrollid = recode.getEnrollid();
            Enroll enroll = enrollService.getById(enrollid);
            recode.setOpus(enroll.getOpus());
        }
        return Result.success(page);
    }

    /**
     * 分页多个条件模糊查询
     * 仅查询自己角色的报名信息
     */
    @GetMapping("/selectByUserPage")
    public Result selectByUserPage(
            @RequestParam Integer pageNum,//页数或者页码
            @RequestParam Integer pageSize,//当前页总共有多少条数据 这两个都是前端传参过来的
            @RequestParam String title,
            @RequestParam String sign,
            @RequestParam String teacher
    ) {//得到数组，并传到service中
        User currentUser = TokenUtils.getCurrentUser(); //获取当前登录的用户信息
        Integer partid = currentUser.getId();
        QueryWrapper<Grade>queryWrapper = new QueryWrapper<Grade>().orderByDesc("id");//默认倒序,让最新的数据在最上面
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.like(StrUtil.isNotBlank(sign), "sign", sign);
        queryWrapper.like(StrUtil.isNotBlank(teacher), "teacher", teacher);
        queryWrapper.like( "partid", partid);
        Page<Grade> page = gradeService.page(new Page<>(pageNum, pageSize), queryWrapper);//service中的函数
        List<Grade> records = page.getRecords();
        for (Grade recode : records){
            Integer enrollid = recode.getEnrollid();
            Enroll enroll = enrollService.getById(enrollid);
            recode.setOpus(enroll.getOpus());
        }
        return Result.success(page);
    }

}
